package com.learning.javaweb_springboot_jwt.controller;

import com.learning.javaweb_springboot_jwt.entity.LoginRequest;
import com.learning.javaweb_springboot_jwt.util.JwtUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AuthController {

    @PostMapping("/login")
    public String login(@RequestBody LoginRequest loginRequest) {
        // 在这里验证用户名和密码
        if (isValidUser(loginRequest.getUsername(), loginRequest.getPassword())) {
            return JwtUtil.generateToken(loginRequest.getUsername());  // 生成JWT

        }
        throw new RuntimeException("Invalid credentials");
    }

    //用于上面调用
    private boolean isValidUser(String username, String password) {
        // 验证用户名和密码逻辑,  店前和括号内的比较
        return "11".equals(username) && "111".equals(password);
    }
}

